<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.busname" placeholder="业务类型名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.busCode" placeholder="业务类型代码"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getBusTypePage">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--private String busuuid;-->

		<!--列表-->
		<el-table :data="bustypes" highlight-current-row v-loading="listLoading"  border max-height="620"  style="width: 100%;">
			<el-table-column type="index" width="80" label="编号">
			</el-table-column>
			<el-table-column prop="busname" label="业务类型名称" width="180" >
			</el-table-column>
			<el-table-column prop="busCode" label="业务类型代码" width="180"   >
			</el-table-column>
			<el-table-column prop="status" label="状态" width="100" :formatter="formatStatus"    >
			</el-table-column>
			<el-table-column prop="createtime" label="创建时间" width="180"  >
			</el-table-column>
			<el-table-column prop="remark" label="备注" min-width="220"  >
			</el-table-column>
			<el-table-column label="操作" width="90">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">{{scope.row.status == "1" ? '禁用' : '启用'}}</el-button>
					<!--<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>-->
				</template>
			</el-table-column>
		</el-table>

		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>
		<!--编辑界面-->
<!--		<el-dialog title="编辑业务类型" v-model="editFormVisible" :close-on-click-modal="false">-->
<!--			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">-->
<!--				<el-form-item label="名称" prop="busname">-->
<!--					<el-input v-model="editForm.busname" auto-complete="off"></el-input>-->
<!--				</el-form-item>-->

<!--				<el-form-item label="代码" prop="buscode">-->
<!--					<el-input-number v-model="editForm.busCode" :min="0" :max="200"></el-input-number>-->
<!--				</el-form-item>-->

<!--				<el-form-item label="备注">-->
<!--					<el-input type="textarea" v-model="editForm.remark"></el-input>-->
<!--				</el-form-item>-->
<!--			</el-form>-->
<!--			<div slot="footer" class="dialog-footer">-->
<!--				<el-button @click.native="editFormVisible = false">取消</el-button>-->
<!--				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>-->
<!--			</div>-->
<!--		</el-dialog>-->
		<!--新增界面-->
		<el-dialog title="新增业务类型" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="名称" prop="busname">
					<el-input v-model="addForm.busname"  ></el-input>
				</el-form-item>
				<el-form-item label="代码" prop="busCode">
					<el-input v-model="addForm.busCode"  ></el-input>
				</el-form-item>

				<el-form-item label="备注">
					<el-input type="textarea" v-model="addForm.remark"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	import { getBusTypePage,addBussinesType,updateBussinesType } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
                    busname: '',
                    busCode:''
				},
				bustypes: [],
                total: 0,
                start:0,
                pageSize:10,
				listLoading: false,
                isShow: JSON.parse(sessionStorage.getItem("user")).usertype == 1 ? true : false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
                    busname: [
                        { required: true, message: '请输入业务类型名称', trigger: 'blur' },
                        { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' }
					],
                    busCode:[
                        { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' },
                        { required: true, message: '请输入业务类型代码', trigger: 'blur' }
                        ]
				},
				//编辑界面数据
				editForm: {
                    busname: '',
                    busCode: '',
                    remark: ''
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
                    busname: [
						{ required: true, message: '请输入业务类型名称', trigger: 'blur' },
                        { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' }
					],
                    busCode:[
                        { min: 1, max: 30, message: '长度在 1 到 20 个字符', trigger: 'blur' },
                        { required: true, message: '请输入业务类型代码', trigger: 'blur' }
                       ]
				},
				//新增界面数据
				addForm: {
                    busname: '',
                    busCode: '',
                    remark: ''
				}

			}
		},
		methods: {
			//状态显示转换
            formatStatus: function (row, column) {
				return row.status == "1" ? '正常' : row.status == "0" ? '禁用' : '未知';
			},
            getBusTypePage() {
				let para = {
                    start: this.start,
                    pageSize:this.pageSize,
                    busname: this.filters.busname,
                    busCode: this.filters.busCode
				};
				this.listLoading = true;
                getBusTypePage(para).then((datas) => {
					this.listLoading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.bustypes = data.result;
                        this.total = data.totalCount;
                    }
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					removeUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {

				});
			},

			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
                    busname: '',
                    busCode: '',
                    remark: ''
				};
			},
            //显示编辑界面
            handleEdit: function (index, row) {
                // this.editFormVisible = true;
                // this.editForm = Object.assign({}, row);
                this.editSubmit(row);
            },
			//编辑
			editSubmit (row) {
				this.$confirm('确认提交吗？', '提示', {roundButton:true}).then(() => {
					let para = row
					console.log(row)
					para.status = para.status == "1" ? "0" : "1";
					updateBussinesType(para).then((res) => {
						this.$message({
							message: '修改成功',
							type: 'success'
						});
						this.getBusTypePage();
					});
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = Object.assign({}, this.addForm);
//							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            addBussinesType(para).then((datas) => {
								this.addLoading = false;
                                let { message, code, data } = datas;
                                if (code !== "0") {
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getBusTypePage();
							}).catch(function (error) {
                                console.log(error);
                                this.addLoading = false;
                            });

						});
					}
				});
			}
			,handleCurrentChange:function(val){
				this.start = val - 1;
				this.getBusTypePage();
			},
			handleSizeChange:function(val){
				this.pageSize = val;
				this.getBusTypePage();
			}

		},
		mounted() {
			this.getBusTypePage();
		}
	}

</script>

<style scoped>

</style>
